// packageTab/pages/mentor/mentor.js
import {
  top_submembers,
  update_memberlevel_manual,
  count_memberlevel_manual
} from '../../../utils/imapi.js';
const app = getApp();
Page({
  data: {
    list:[],  //接口返回的数据
	scrollTop:0,  
	page_size:13,
	hidden:false,
	index:0,
	name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    const {data}=await top_submembers({page_index:1,page_size:13});
	//console.log(data);
	this.setData({
		list:data
	});
	this.count();

  },
  async count(){
	  const count=await count_memberlevel_manual();
	  //console.log(count);
	  this.setData({
		  index:count.data
	  })
  },
 inputedit(e) {
 	var value = e.detail.value;
 	this.setData({
 		name: value
 	})
 },
  upgrade(e){
	  console.log(e);
	  if(e.currentTarget.dataset.item.level_code==3){
		  wx.showModal({
		  		title: '提示',
		  		content: '会员等级已为最高',
		  	})
			return;
	  }else if(e.currentTarget.dataset.item.level_code==2){
		
	  }else{
		  var level_code=e.currentTarget.dataset.item.level_code;
		  var id=e.currentTarget.dataset.item.id;
		  var code_id=String(Number(e.currentTarget.dataset.item.level_code)+1);
		  wx.showModal({
		  		title: '提示',
		  		content: '把'+e.currentTarget.dataset.item.name+'升级为导师？',
		  		success:(res)=>{
					if(res.cancel){
						
					}else{
						this.upda('2',id,code_id);
					}
		  	}
		  
	  })
	  
    }
  },
  async upda(level_code,id,code_id){
	  const data = await update_memberlevel_manual({level_code:level_code,member_id:id,from_level_code:code_id});
	  console.log(data);
	  if(data.code==0){
          if(data.data==-1){
			  wx.showModal({
			  		title: '提示',
			  		content:  data.msg
			  })
			  // console.log('测试的');
			  // const date=await top_submembers({page_index:1,page_size:this.data.page_size});
			  // console.log(date);
			  // this.setData({
			  // 	list:date.data
			  // });
		  }else{
			  wx.showToast({
			      title: '操作成功！',  // 标题
			      icon: 'success',   // 图标类型，默认success
			      duration: 1500   // 提示窗停留时间，默认1500ms
			  })
			  const date=await top_submembers({page_index:1,page_size:this.data.page_size});
			  console.log(date);
			  this.setData({
			  	list:date.data
			  });
			  this.count();
		  }
	  }else{
		  wx.showToast({
		      title: data.msg,
		      icon: 'none',
		      duration: 1500
		  })
	  }
	  
  },
  async search(){
	  if(this.data.name==''){
		  const date=await top_submembers({page_index:1,page_size:this.data.page_size});
		  console.log(date);
		  this.setData({
		  	list:date.data
		  });
		  return;
	  }
	  const data=await top_submembers({nickname:this.data.name,page_index:1,page_size:1});
	  //console.log(data);
	  this.setData({
	  	list:data.data
	  });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom:async function () {
	  console.log('拉下')
              this.setData({
		          hidden:true
		      });
			  const {data}=await top_submembers({page_index:1,page_size:this.data.page_size+10});
		   	setTimeout(()=>{
				 this.setData({
					    list:data,
						hidden:false,
						page_size:this.data.page_size+10
				  });
			},1000);	
		     
		   console.log("lower");
  },
 onPageScroll(e) {
    console.log(e) //滚动条 滚动的位置（e.scrollTop）从头部开始计算
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})